package com.survey.demo.ui.activity;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.survey.demo.R;
import com.survey.demo.aop.SingleClick;
import com.survey.demo.app.AppActivity;
import com.survey.demo.http.bean.AddressBean;
import com.survey.demo.manager.RecordDao;
import com.survey.demo.manager.RecordSQLiteOpenHelper;
import com.survey.demo.widget.SpaceItemDecoration;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 搜索界面
 */
public final class SearchActivity extends AppActivity {
    private EditText mHintView;
    private RecyclerView hot_recy;
    private RecyclerView history_recy;
    private RecordSQLiteOpenHelper helper;
    private BaseQuickAdapter history_adapter;
    private  RecordDao dao;
    @Override
    protected int getLayoutId() {
        return R.layout.search_activity;
    }

    @Override
    protected void initView() {
        helper = RecordSQLiteOpenHelper.getInstance();
        dao = new RecordDao();
        mHintView = findViewById(R.id.tv_home_hint);
        hot_recy = findViewById(R.id.hot_recy);
        hot_recy.addItemDecoration(new SpaceItemDecoration(2, 38, true));
        history_recy = findViewById(R.id.history_recy);
        history_recy.setLayoutManager(new GridLayoutManager(this,5));
        //history_recy.addItemDecoration(new SpaceItemDecoration(2, 38, true));
        List<String> ss =dao.queryData("");
        history_adapter =new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_search, ss) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, String str) {
                TextView name =  (TextView)baseViewHolder.getView(R.id.tv_name);
                name.setText(str);
            }
        };
        history_recy.setAdapter(history_adapter);
        mHintView.setOnKeyListener(new View.OnKeyListener() {// 输入完后按键盘上的搜索键

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {// 修改回车键功能
                    Tosearch();
                }
                return false;
            }
        });
        setOnClickListener(R.id.search_btn);
    }

    @Override
    protected void initData() {

    }

    @SingleClick
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.search_btn:
                Tosearch();
                break;
        }
    }
    private void Tosearch(){
        // 先隐藏键盘
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        // 按完搜索键后将当前查询的关键字保存起来,如果该关键字已经存在就不执行保存
        boolean hasData = dao.hasData(mHintView.getText().toString().trim());
        if (!hasData) {
            dao.insertData(mHintView.getText().toString().trim());
            //history_adapter.setNewData(dao.queryData(""));
        }
        // TODO 根据输入的内容模糊查询商品，并跳转到另一个界面，由你自己去实现
        // Toast.makeText(MainActivity.this, "clicked!", Toast.LENGTH_SHORT).show();
    }
}